package com.weixing.mall.provider.support.excalservice;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.api.model.vo.ProductStockVo;
import com.weixing.mall.provider.model.domain.DefectiveProduct;
import com.weixing.mall.provider.model.domain.ProductStockDetails;
import com.weixing.mall.provider.service.IDefectiveProductService;
import com.weixing.mall.provider.service.IDepotExtraService;
import com.weixing.mall.provider.service.IProductStockDetailsService;
import com.weixing.mall.provider.support.excaldata.StockCodeData;
import com.weixing.mall.util.ListUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 次品库导入数据处理
 *
 * @date 2020-6-6
 */
public class DefectiveStockCodeDataListener extends AnalysisEventListener<StockCodeData> {
    private static final Logger LOGGER = LoggerFactory.getLogger(DefectiveStockCodeDataListener.class);
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 5;

    List<StockCodeData> list = new ArrayList<StockCodeData>();

    private IDefectiveProductService productStockService;
    private IDepotExtraService depotExtraService;
    /**
     * 业务数据组装
     */
    private List<ProductStockVo> stockList;

    /**
     * 业务参数
     */
    private Long depotId;

    private DefectiveStockCodeDataListener() {

    }

    public DefectiveStockCodeDataListener(Long depotId, IDefectiveProductService productStockService, IDepotExtraService  depotExtraService) {
        this.depotId = depotId;
        this.productStockService = productStockService;
        this.depotExtraService = depotExtraService;

    }

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param stockCodeData
     * @param analysisContext
     */
    @Override
    public void invoke(StockCodeData stockCodeData, AnalysisContext analysisContext) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(stockCodeData));
        list.add(stockCodeData);
    }


    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        LOGGER.info("{}条数据，开始处理数据！", list.size());
        List<DefectiveProduct> productStockList=Lists.newArrayList();
        for(StockCodeData stockCodeData:list){
            DefectiveProduct productStockDetails=new DefectiveProduct();
            productStockDetails.setId(stockCodeData.getId());

            productStockDetails.setStockSn(stockCodeData.getDepotSn());
            productStockList.add(productStockDetails);
        }
        productStockService.updateBatchById(productStockList, ListUtil.SIZE);
    /*List<DepotExtra> depotExtraList=Lists.newArrayList();
     for(StockCodeData stockCodeData:list){
         String color=stockCodeData.getClor();
         String size=stockCodeData.getSize();
         String goodSn=stockCodeData.getGoodSn();
         for(ProductStockVo productStockVo:stockList){
             String conColor=SKUUtils.color2(productStockVo.getProductSkuDesp());
             String conSize=SKUUtils.size2(productStockVo.getProductSkuDesp());
             String conGoodSn=productStockVo.getGoodsSn();
             if(color.equals(conColor)&&size.equals(conSize)&&goodSn.equals(conGoodSn)){

                 if(null!=stockCodeData.getStroehouse()){
                     ProductStock stock=new ProductStock();
                     stock.setStorehouse(stockCodeData.getStroehouse());
                     stock.setId(productStockVo.getId());
                     productStockList.add(stock);
                 }

                 DepotExtra depotExtra=new DepotExtra();
                 depotExtra.setDepotId(depotId);
                 depotExtra.setDepotLocation(stockCodeData.getStroehouse());
                 depotExtra.setProductId(productStockVo.getProductId());
                 depotExtra.setDepotSn(stockCodeData.getDepotSn());
                 depotExtraList.add(depotExtra);
             }
         }
     }
     if(productStockList.size()>0){
         productStockService.updateBatchById(productStockList);
     }

     if(depotExtraList.size()>0){
         depotExtraService.saveBatch(depotExtraList);
     }*/
        LOGGER.info("数据处理完毕");
    }

}
